﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DeepZoomPivotConstructor.VisualLib
{
    /// <summary>
    /// Some helper (extension) functions for dealing with visual trees.
    /// </summary>
    public static class VisualUtilities
    {
        /// <summary>
        /// Counts the number of composable units satisfying some predicate
        /// </summary>
        /// <param name="input">The visual tree to walk</param>
        /// <param name="predicate">The test to apply to each element in the visual tree, counding only those that are good</param>
        /// <returns></returns>
        public static int CountNumberOfComposableUnits(this Visual input, Func<Visual, bool> predicate)
        {
            var allCUs = input.GetHierarchicalEnumerator(v => v.IsCompositionUnit).Where(v => v.IsCompositionUnit);
            return allCUs.Count(predicate);
        }

        /// <summary>
        /// Counts the number of composable units in the visual hierarchy.
        /// </summary>
        /// <param name="input">The visual tree to scan for CU's</param>
        /// <returns></returns>
        public static int CountNumberOfComposableUnits(this Visual input)
        {
            return input.CountNumberOfComposableUnits(v => true);
        }
    }
}
